var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var expFiniLogin = SA.get('USER');
var exfdata = false;
var _expressId = '';

var bindEvent = function(){
    //此处各种dom 操作
    //         //APPOINTMENT("已预约", "APPOINTMENT")
    //         //WAITFORPAY("待支付", "WAITFORPAY")
    //         //PAID("已支付", "PAID")
    //         //CANCEL("已取消", "CANCEL")
    //         //COMPLETE("已完成", "COMPLETE")
    //         //TOEVALUATE("待评价", "TOEVALUATE")
    //         //COMPLETEEVALUATE("已评价", "COMPLETEEVALUATE")
    //         //ORDERCOMPLETE("订单完成", "ORDERCOMPLETE")
    $('#expItemLog').click(function(){
        router('water/status', {back: exfdata.order.orderId})
    })
    $('.comments a').click(function(){
        router('water/comments', {backid: exfdata.order.orderId, backstatus: exfdata.order.status})
    })
    $('.send-exp-list .block').click(function(){
        var exp = $(this).find('.exp').text();  //快递公司名称
        var bill = $(this).find('.bill').text(); //快递单号
        var _orderId = exfdata.order.orderId;
        localStorage.setItem('savehistory',JSON.stringify(SA.get('_HISTORY')))
        router('http://m.kuaidi100.com/index_all.html?type='+exp+'&postid='+bill+'&callbackurl=http://192.168.1.43:3000/app?hash=send/details&orderId='+_orderId.orderId)
    });
};

var ExpFinish = React.createClass({
    mixins: [ItemMixin],
    componentWillMount: function(){
        if(this.props.data){
            this.setState({
                exfData: this.props.data
            })
        }
    },
    willMount: function(){
        if(this.state.exfData.order.status == 'COMPLETEEVALUATE'){
            return(
                <div className="f_submit mT01 comments"  id="comments_off">
                    <a href="javascript:;" className="f_btn comments_btn evalBtn">查看评价</a>
                </div>
            )
        }else if(this.state.exfData.order.status == 'TOEVALUATE'){
            return(
                <div className="f_submit mT01 comments" id="comments_on">
                    <a href="javascript:;" className="f_btn comments_btn evalBtn">评价</a>
                </div>
            )
        }
    },
    render:function(){
        var exfComments = this.willMount();
        var exfItem_data = this.state.exfData;
        var items = [];
        var exfPay = []
        if(exfItem_data){
            //列表
            exfItem_data.pkgList.map(function(item,i){
                items.push(
                    <li className="send-exp-list paddingL_no">
                        <div className="block">
                            <div className="img"><img src={item.imgUrl||'/images/express/icon_express.png'} /></div>
                            <div className="com"><span className="exp">{item.expressName}</span>－<span className="bill">{item.bill}</span></div>
                        </div>
                    </li>
                );
            });
            //支付方式
            if(exfItem_data.order.payType == 'PAY_ON_DELIVERY'){
                exfPay.push(
                    <li>
                        <div className="dP_name">
                            <i className="iconfont icon-fukuan"></i>
                            <span>货到付款</span>
                        </div>
                    </li>
                )
            }else{
                exfPay.push(
                    <li>
                        <div className="dP_name">
                            <i className="iconfont icon-weixinzhifu"></i>
                            <span>微信支付</span>
                        </div>
                    </li>
                )
            }
            //组合成预约时间
            var sdcreateTime = exfItem_data.orderLog ? exfItem_data.orderLog.createTime.substr(0,16):'';
            var endTimes = exfItem_data.order.endTime.substr(0,16)
            var endtimedata = endTimes.split(" ");
            var exfItem_time = exfItem_data.order.startTime.substr(0,16) + '~' + endtimedata[1];
            console.log(exfItem_data);
        }
        return (
            <div className="expFinishWarp">
                <div className="defalut_head_orders default_pd016 box-shadow exp_log" id="expItemLog">
                    <em>
                        {  (function(){
                                if (!exfItem_data.orderLog){
                                    return '';
                                }
                                var _d = exfItem_data.orderLog.description,
                                    _c = exfItem_data.orderLog.createBy;
                                return _d.indexOf('领单')>-1 ? _c+''+_d: _d;
                            })()
                        }
                    </em>
                    <div className="dHO_link">
                        <p>
                            {sdcreateTime}
                        </p>
                        <a href="javascript:;" className="link_more">查看更多 <i className="iconfont icon-xiangyou"></i></a>
                    </div>
                </div>
                {exfComments}
                <ul className="default_form mT014 box-shadow">
                    <li>
                        <label>订单号</label>
                        <p className="default_no_update receiverName">{exfItem_data.order.orderNumber}</p>
                    </li>
                    <li>
                        <label>姓名</label>
                        <p className="default_no_update receiverName">{exfItem_data.order.contact}</p>
                    </li>
                    <li>
                        <label>电话</label>
                        <p className="default_no_update receiverPhone">{exfItem_data.order.contactPhone}</p>
                    </li>
                    <li>
                        <label>地址</label>
                        <p className="default_no_update receiverAddress">{exfItem_data.order.contactAddress}</p>
                    </li>
                    <li>
                        <label>预约时间</label>
                        <p className="default_no_update deliveryBookTime">{exfItem_time}</p>
                    </li>
                </ul>
                <div className="default_pd016 mT014 box-shadow"  style={exfItem_data.pkgList.length>0?{display:'block'}:{display:'none'}}>
                    <ul>
                        {items}
                    </ul>
                    <div className="default_Courier_money">
                        <p>服务费：<i className="charge">{'¥ '+exfItem_data.order.summoney}</i></p>
                    </div>
                </div>
                <div className="default_pay box-shadow">
                    <ul>
                        {exfPay}
                    </ul>
                </div>
            </div>
        )
    }
});

function start(name){
    return pages.new({
        boot: function(){
        },
        trigger:function(self, data){
            this.libs.changeTitle('订单详情');    //更改当前页面标题
            // if(localStorage.getItem('savehistory')){         //获取存储跳转到h5页面前的记录，回退
            //     SA.set('_HISTORY',JSON.parse(localStorage.getItem('savehistory')))
            //     localStorage.removeItem('savehistory')
            // }
            if(data && data.back){
                sessionStorage.setItem('_expressid', data.back)
                _expressId = data.back;
            }
            else {
                _expressId = sessionStorage.getItem('_expressid')
                if (_expressId){
                    SA.set('_HISTORY', JSON.parse(localStorage.getItem('savehistory')))
                }
            }
            if (_expressId){
                expressitem_post = {
                    login: SA.get('USER').login,
                    orderId: _expressId,
                    method: 'get'
                }
                api.req('/exp_finish', expressitem_post, function(data){
                    self.main(self, data)
                })
            }
            else {
                libs.msgtips('订单不存在', 'center', function(){
                    router.goback()
                })
            }
        },
        main: function(self, data){
            if(data){
                exfdata = data
            }
            this.render(
                <ExpFinish itemDefaultMethod={bindEvent} data={data} />,
                document.getElementById(name)
            )
        },
        end: function(){
            // sessionStorage.removeItem('_expressid')
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
